const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    price: '',
    totalprice: '',
    restaurantId: '',
    types:'',
    list: {},
    urls: '',
    billId: '',
    order: '',
    address: [],
    showBottomPopup: false,//底部弹框
    addrList: [],
    textDis: true,
    types:'',
    addressid:'',
    total:"",
    logfee:'',//物流费
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log('zhangdan',options.billId)
    let price = options.price
    let totalprice = options.totalprice
    let billId = options.billId
    let restaurantId = options.restaurantId
    let order = options.order
    let types = options.types
    // console.log(types)
    // console.log('dimngdada' + order)
    this.setData({
      price: price,
      totalprice: totalprice,
      restaurantId: restaurantId,
      billId: billId,
      order: order,
      types:types
    })
    this.userDefulAddress(billId, types)
    this.shop_card(billId)

    // this.getRestaurantInfo(restaurantId)
  },
  //获取地址
  userDefulAddress(billId, types) {
    let _this = this
    var userId = app.globalDataO.openid.objectSid
    app.post(app.url.userDefulAddress, {
      wechatId: userId,
      bill_id: billId,
      type: types
    }).then(res => {
      // console.log(res)
      if (res.data.address.length == 0) {
        return
      }
      var addressid = res.data.address[0].id
      // console.log('asdadasdasdas',addressid)
      _this.setData({
        address: _this.filterBlank(res.data.address),
        addressid: addressid
      })
      _this.getlogfee(addressid)
    })
    
    
  },
  /**
   * 过滤空格和-
   */
  filterBlank(e) {
    let str = e[0].address
    str = str.replace(/\s+/g, "")
    str = str.replace('-', "")
    str = str.replace('-', "")
      e[0].address = str
    return e
  },
  //查看购物车
  shop_card(billId) {
    var that = this
    app.post(app.url.billLine, {
      billId: billId
    }).then(res => {
      // console.log(res.data)
      if (res.data.billLine == "" || res.data.billLine == null) {
        return false;
        wx.showToast({
          title: '出错啦',
        })
      } else {
        that.setData({
          list: res.data,
          urls: res.data.urls
        })
      }
    })
  },
  //获取物流费
  getlogfee(addressid) {
    var total =Number(this.data.totalprice)
    // console.log(total)
    app.post(app.url.logfeeprice, {
      restaurant_id: this.data.restaurantId,
      bill_id: this.data.billId,
      addressId: addressid
    }).then(res => {
      var logfee = Number(res.data.logfee)
      this.setData({
        logfee: res.data.logfee,
        total: total+logfee
      })
      // console.log(this.data.total)
    })

  },
  /**
   * 选择收货地址
   */
  selectAddr(e) {
    let addrs = []
    addrs.push(e.currentTarget.dataset.addr)
    var addressid = e.currentTarget.dataset.addid
    this.getlogfee(addressid)
    this.setData({
      address: this.filterBlank(addrs),
      showBottomPopup: false,
      textDis: true
    })
// console.log(this.data.address)
  },

  /**
	 * 添加
	 */
  add: () => {
    wx.navigateTo({
      url: '../add_address/address',
    })
  },

  /**
	 * 修改
	 */
  revise: function (e) {
    let item = e.currentTarget.dataset.item
    // console.log('跳转参数', item)
    wx.navigateTo({
      url: '../revise_address/address?address=' + item,
    })
  },


  /**
   * 获得所有收货地址
   */
  toggleBottomPopup() {
    this.setData({
      showBottomPopup: !this.data.showBottomPopup,
      textDis: !this.data.textDis
    })
  },

  /**
   * 取消选择收货地址
   */
  addressCancel() {
    this.setData({
      showBottomPopup: false,
      textDis: true
    });
  },

  // 获取个人收货地址
  getSelfAddress: function () {
    let getData = {
      userId: app.globalDataO.openid.objectSid,
    }
    app.post(app.url.address.getSelfAddress, getData).then(re => {
      if (re.statusCode == 200) {
        this.setData({
          addrList: re.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getSelfAddress()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})